var $, element, layer, laydate, form, table;
var unitId = '';
var businessId = '';
// 加载元素控件
layui.use(['jquery', 'element', 'layer', 'laydate', 'form', 'table'], function () {
    $ = layui.jquery, element = layui.element, layer = layui.layer, laydate = layui.laydate, form = layui.form, table = layui.table;
    layer.tips('选择商圈', '#dd');
    $.ajax({
        url: "/user/findone",
        data: {},
        dataType: "json",
        type: "post",
        async: false,
        success: function (res) {
            if (res.errcode === "0") {

                var host = location.host;
                $(".layui-nav-img").attr("src", "http://" + host + "/" + res.data.IMG_URL);

                $(".userName").html(res.data.NAME);

            } else if (res.errcode === "1") {
                layer.msg(res.errmsg, function () {
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest.status);
            console.error(XMLHttpRequest.readyState);
            console.error(textStatus);
        }
    });
    $('.fa').click(function () {
        var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
        parent.layer.close(index); // 再执行关闭
    });
    //消息
    $("#message").click(function () {
        var index = layer.open({
            //maxmin: true,
            zIndex: 1002,
            //offset: 'b',
            type: 2,    //弹出类型
            shadeClose: true,
            title: '消息',   //标题
            closeBtn: 1,    //显示关闭按钮
            shade: [0.3],   //遮罩透明度
            area: ['65%', '90%'],  //打开窗口的宽高
            time: 0,    //几秒后自动关闭，默认0时不关闭
            anim: 2,    //动画效果
            resize: false,  //设置成不允许拉伸
            content: '../../webhtml/message/message.html'  //iframe的url
        });
    });
    //待处理账单 监听工具条
    table.on('tool(szlbf)', function (obj) {
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'detail') {
            layer.open({
                zIndex: 1000,
                type: 2,    //弹出类型
                title: '查看账单',   //标题
                closeBtn: 1,    //显示关闭按钮
                shade: [0.3],   //遮罩透明度
                area: ['80%', '90%'],  //打开窗口的宽高
                time: 0,    //几秒后自动关闭，默认0时不关闭
                anim: 1,    //动画效果
                resize: false,  //设置成不允许拉伸
                content: '../../../webhtml/contract/bill_check.html?BILL_ID=' + data.ID + ''  //iframe的url
            });
        } else if (layEvent === 'dclsf') {
            layer.confirm('确定收费？', function (index) {
                layer.close(index);
                parent.layer.open({
                    zIndex: 1000,
                    type: 2,    //弹出类型
                    title: '收费',   //标题
                    closeBtn: 1,    //显示关闭按钮
                    shade: [0.3],   //遮罩透明度
                    area: ['80%', '90%'],  //打开窗口的宽高
                    time: 0,    //几秒后自动关闭，默认0时不关闭
                    anim: 1,    //动画效果
                    resize: false,  //设置成不允许拉伸
                    content: '../../../webhtml/contract/charge.html?BILL_ID=' + data.ID + ''  //iframe的url
                });
                $(this).blur(); //移除焦点，防止点击回车按钮造成多次打开
            });
        }
    });
    /**
     * 打开 添加客户
     */
    $('.add_customer').click(function () {
        layer.open({
            type: 2
            , title: '新建客户'
            , anim: 3
            , shadeClose: true
            , area: ['65%', '90%']
            , content: '../../../webhtml/customer/Customer_add.html?aa=' + $(this).text() + ''//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            , yes: function (index, layero) {
                //do something
                //layer.closeAll();
                reloadTablezs();
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            }
        });
    });
    //测试数据
    var activebuildingID = '';//选中楼宇ID
    var activebusinessID = '';//选中商圈ID
    //监听tab 切换导航数据
    var iframe = document.createElement("iframe");

    iframe.style.width = "100%";
    iframe.style.height = (document.documentElement.clientHeight-80)+"px";//高度自适应
    iframe.setAttribute('frameborder', '0');

    loadUnit(activebuildingID);

    var tabselect;
    //集合     标签页
    element.on('tab(tabiframe)', function (data) {
        tabselect = $(this).text();
        switch (tabselect) {
            case "收入管理":
                $(".list___2Hba-").find(".lia").removeClass("activebuilding");
                //加载商圈楼宇 合同数据
                activebuildingID = "";
                activebusinessID=$(".nav-show a i").find("input").val();
                var div = document.getElementById("rentiframe");
                iframe.src = '../../../webhtml/rent/rent_list.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                ;
                div.appendChild(iframe);
                break;
            case "楼宇管理":
                //计算选中商圈的数据
                var div = document.getElementById("buildiframe");
                iframe.src = '/building/goRoom.do?&BUILDING_ID=' + activebuildingID + '&UNIT_ID=' + unitId;
                div.appendChild(iframe);
                break;
            case "合同管理":
                //fixme 楼宇管理 转向点击合同管理的时候 取消掉选中楼宇 重新加载商圈下的合同数据
                $(".list___2Hba-").find(".lia").removeClass("activebuilding");
                //加载商圈楼宇 合同数据
                activebuildingID = "";
                // activebusinessID = $(".businessID").val();//老版本获取商圈的值
                activebusinessID=$(".nav-show a i").find("input").val();
                console.log(activebusinessID)
                var div = document.getElementById("contractiframe");
                iframe.src = '../../../webhtml/contract/contract_list.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                iframe.id = 'htIframe';
                div.appendChild(iframe);
                break;
            case "抄表管理":
                var div = document.getElementById("meteriframe");
                iframe.src = '../../../webhtml/hydropower/meter_list.html?activebuildingID=' + activebuildingID+ '&activebusinessID=' + activebusinessID;
                div.appendChild(iframe);
                break;
            case "招商管理":
                $(".list___2Hba-").find(".lia").removeClass("activebuilding");
                activebuildingID = "";
                var div = document.getElementById("customeriframe");
                iframe.src = '../../../webhtml/customer/Customer_list.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                div.appendChild(iframe);
                break;
            case "合同类":
                var div = document.getElementById("DaiBancontractiframe");
                iframe.src = '../../../webhtml/daiban/contract_dblist.html?activebuildingID=' + activebuildingID;
                div.appendChild(iframe);
                break;
            case "账单类":
                activebuildingID = "";
                activebusinessID=$(".nav-show a i").find("input").val();
                var div = document.getElementById("DaiBanbilliframe");
                iframe.src = '../../../webhtml/daiban/bill_dblist.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                div.appendChild(iframe);
                break;
            case "停车场管理":
                var div = document.getElementById("depotiframe");
                iframe.src = '../../../webhtml/depot/depot_list.html?activebuildingID=' + activebuildingID;
                div.appendChild(iframe);
                break;
        }
    });
    //审批事项 消息 标签页
    element.on('tab(daiban)', function (data) {
        tabselect = $(this).text();
        activebusinessID=$(".nav-show a i").find("input").val();
        if ("合同类" == tabselect) {
            var div = document.getElementById("DaiBancontractiframe");
            iframe.src = '../../../webhtml/daiban/contract_dblist.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
            div.appendChild(iframe);
        }
        if ("招商类" == tabselect) {

        }
        if ("账单类" == tabselect) {
            var div = document.getElementById("DaiBanbilliframe");
            iframe.src = '../../../webhtml/daiban/bill_dblist.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
            div.appendChild(iframe);
        }

    });
    //贷款合同 标签页
    element.on('tab(daikuan)', function (data) {
        var dataname = $(this).data("name");
        //贷款首页
        switch (dataname) {
            case 1:
                //贷款主页
                var div = document.getElementById("daikuanIframe");
                iframe.src = '../../../webhtml/contract/loan_main.html?';
                div.appendChild(iframe);
                break;
            case 2:
                //贷款合同 招商
                var div = document.getElementById("ZhaoShangIframe");
                iframe.src = '../../../webhtml/customer/Loan_Customer_list.html?';
                div.appendChild(iframe);
                break;
            case 3:
                //贷款合同管理
                var div = document.getElementById("DKContractIframe");
                iframe.src = '../../../webhtml/contract/loan_list.html';
                div.appendChild(iframe);
                break;
            case 4:
                //收租明细 收入
                var div = document.getElementById("LoanBillIframe");
                iframe.src = '../../../webhtml/rent/loan_rent_list.html';
                div.appendChild(iframe);
                break;
        }
    });
    //监听 选中贷款金额数
    $(".loan_price").click(function () {
        $(this).find("span").html();
        var loan_price=$(this).find("span").html();
        // $(".layui-this").click();
        var tabselect=$(".layui-tab-title .layui-this").eq(2).html();
        switch (tabselect) {
            case "贷款管理":
                //贷款主页
                var div = document.getElementById("daikuanIframe");
                iframe.src = '../../../webhtml/contract/loan_main.html?loan_price='+loan_price;
                div.appendChild(iframe);
                break;
            case "招商管理":
                //贷款合同 招商
                var div = document.getElementById("ZhaoShangIframe");
                iframe.src = '../../../webhtml/customer/Loan_Customer_list.html?loan_price='+loan_price;
                div.appendChild(iframe);
                break;
            case "合同管理":
                //贷款合同管理
                var div = document.getElementById("DKContractIframe");
                iframe.src = '../../../webhtml/contract/loan_list.html?loan_price='+loan_price;
                div.appendChild(iframe);
                break;
            case "收入管理":
                //收入管理 收入
                var div = document.getElementById("LoanBillIframe");
                iframe.src = '../../../webhtml/rent/loan_rent_list.html?loan_price='+loan_price;
                div.appendChild(iframe);
                break;
        }
        //贷款首页
    });

    //加载tab选项卡事件
    function loadtab() {
        // activebusinessID = $(".businessID").val();
        activebusinessID=$(".nav-show a i").find("input").val();
        switch (tabselect) {
            case "收入管理":
                var div = document.getElementById("rentiframe");
                iframe.src = '../../../webhtml/rent/rent_list.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                div.appendChild(iframe);
                break;
            case "楼宇管理":
                //计算选中商圈的数据
                var div = document.getElementById("buildiframe");
                iframe.src = '/building/goRoom.do?&BUILDING_ID=' + activebuildingID + '&UNIT_ID=' + unitId;
                div.appendChild(iframe);
                break;
            case "合同管理":
                var div = document.getElementById("contractiframe");
                iframe.src = '../../../webhtml/contract/contract_list.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                iframe.id = 'htIframe';
                div.appendChild(iframe);
                break;
            case "抄表管理":
                var div = document.getElementById("meteriframe");
                iframe.src = '../../../webhtml/hydropower/meter_list.html?activebuildingID=' + activebuildingID+ '&activebusinessID=' + activebusinessID;
                break;
            case "招商管理":
                var div = document.getElementById("customeriframe");
                iframe.src = '../../../webhtml/customer/Customer_list.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                div.appendChild(iframe);
                break;
            case "合同类":
                var div = document.getElementById("DaiBancontractiframe");
                iframe.src = '../../../webhtml/daiban/contract_dblist.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                div.appendChild(iframe);
                break;
            case "账单类":
                var div = document.getElementById("DaiBanbilliframe");
                iframe.src = '../../../webhtml/daiban/bill_dblist.html?activebuildingID=' + activebuildingID + '&activebusinessID=' + activebusinessID;
                div.appendChild(iframe);
                break;
            case "停车场管理":
                var div = document.getElementById("depotiframe");
                iframe.src = '../../../webhtml/depot/depot_list.html?activebuildingID=' + activebuildingID;
                div.appendChild(iframe);
                break;
        }
    }

    //选中商圈事件
    $(".busid").click(function () {
        // businessId = $(this).find("input").val();//老版本选择商圈
        businessId=$(this).find("input").val();
        $(".businessID").val(businessId);//保存选中的商圈id
        activebuildingID = "";//切换商圈的时候 清除上次的选中楼宇
        loadtab();
        // $(".lia").removeClass("activebuilding");
        $('.list___2Hba-').html('');
        loadbuild.loadbuilding();
    });
    //新版切换商圈事件
    $('.nav-item>a').on('click',function(){
            $(".dk").click();//加载贷款iframe'
            $(".addContract").html('<a href="javascript:void(0);" onclick="toContractAdd(1)"><i\n' +
                '                    class="fa fa-plus" aria-hidden="true"></i></a>');
    });
    var loadbuild = {
        loadbuilding: function () {
            var data = {};
            data.BUSINESS_ID = businessId;
            $.ajax({
                url: "/building/listArr",
                data: data,
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0") {
                        //载入楼宇
                        // background-image 没有图片的时候 请求404
                        for (var i = 0; i < res.data.length; i++) {
                            if (!!res.data[i].IMG) {
                                $(".list___2Hba-").append("<li class=\"lia \">\n" +
                                    "                                            <div class=\"imgBox___WeKZ4\"\n" +
                                    "                                                 style=\"background-image: url(" + res.data[i].IMG + ");\"></div>\n" +
                                    "                                            <div class=\"info___1Dhyq\">\n" +
                                    "                                                <div class=\"title___1pxg0\">" + res.data[i].BUILDING_NAME + "</div>\n" +
                                    "                                                <div class=\"areaSize___1arhZ\"><input type=\"hidden\" value=\"" + res.data[i].BUILDING_ID + "\"/>\n" +
                                    "                                                    <!-- react-text: 4006 -->" + res.data[i].BUILDING_AREA + "\n" +
                                    "                                                    <!-- /react-text --><!-- react-text: 4007 -->㎡\n" +
                                    "                                                    <!-- /react-text --></div>\n" +
                                    "                                            </div>\n" +
                                    "                                        </li>");
                            } else {
                                $(".list___2Hba-").append("<li class=\"lia \">\n" +
                                    "                                            <div class=\"imgBox___WeKZ4\"\n" +
                                    "                                                 style=\"background-image: url(http://p1.qhmsg.com/dm/110_110_100/t012e388512bdcde533.jpg);\"></div>\n" +
                                    "                                            <div class=\"info___1Dhyq\">\n" +
                                    "                                                <div class=\"title___1pxg0\">" + res.data[i].BUILDING_NAME + "</div>\n" +
                                    "                                                <div class=\"areaSize___1arhZ\"><input type=\"hidden\" value=\"" + res.data[i].BUILDING_ID + "\"/>\n" +
                                    "                                                    <!-- react-text: 4006 -->" + res.data[i].BUILDING_AREA + "\n" +
                                    "                                                    <!-- /react-text --><!-- react-text: 4007 -->㎡\n" +
                                    "                                                    <!-- /react-text --></div>\n" +
                                    "                                            </div>\n" +
                                    "                                        </li>");
                            }

                        }
                        $(".lia").click(function () {
                            clear();
                            $(this).addClass("activebuilding");
                        });
                        //选中楼宇事件
                        $(".lia").click(function () {
                            $(".activebuilding").each(function (index) {
                                activebuildingID = $(this).find("input").val();
                                loadUnit(activebuildingID);
                                //模拟点击事件
                                //计算选中商圈的数据
                                loadtab();

                            });
                            //加载首页统计数据  层数 总面积 已租面积 。。。等
                            loadbuild.loadtotal();
                        });

                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        },
        loadtotal: function () {
            $.ajax({
                url: "/building/roomInfo",
                data: {BUILDING_ID: activebuildingID, UNIT_ID: unitId},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0") {
                        var floor = res.floorlist;
                        var room = res.roomList;
                        $(".lc").html(floor.length);//总层数
                        $(".zmj").html(res.BUILDING_AREA);//总面积
                        res.roomzu && $(".yzmj").html(res.roomzu.toFixed(2))||$(".yzmj").html("0");
                        if (null == res.roomzu) {
                            $(".yzbfb").html("0");//已租面积比分比
                            $(".kzmj").html(parseFloat(res.BUILDING_AREA));//空值面积
                            $(".kzbfb").html("0");//空值面积百分比
                        } else {
                            var yzbfb = Percentage(parseInt(res.roomzu), parseInt(res.BUILDING_AREA));
                            $(".yzbfb").html(yzbfb);//
                            $(".kzmj").html((parseFloat(res.BUILDING_AREA) - parseFloat(res.roomzu)).toFixed(2));//空值面积
                            $(".kzbfb").html(PercentageCase(1 - toPoint(yzbfb)));//空值面积百分比
                        }
                        res.COMPANY_IN&&$(".rzqy").html(res.COMPANY_IN);
                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        }
    }
    layui.data.loadbuild = loadbuild;
    //计算百分比
    function Percentage(number1, number2) {
        return (Math.round(number1 / number2 * 10000) / 100.00 + "%");// 小数点后两位百分比

    }
    //把一个数转换成 百分比
    function PercentageCase(number1) {
        return (Math.round(number1 * 10000) / 100).toFixed(2) + '%';
    }

    //百分比转小数
    function toPoint(percent) {
        var str = percent.replace("%", "");
        str = str / 100;
        return str;
    }

    //默认加载楼宇管理
    $(document).ready(function () {
        $(".jh").click();//加载集合
        $(".busid:first").click();//加载第一个商圈
        $(".list___2Hba- .lia:first").click();//加载第一个楼宇
        $("ul li:nth-child(0)").trigger("click");//加载第一个元素
    });
    //监听工具条  客户管理 编辑 删除 数据
    table.on('tool(zsgl)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确认删除 ' + data.CONTACT + ' 吗', {offset: '150px'}, function (index) {
                layer.close(index);
                $.ajax({
                    url: "/customer/delete",
                    data: {ID: data.ID, IS_DEL: 1},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
                            //reloadTable();    // 表格刷新
                            layer.msg(data.CONTACT + " 已删除", {offset: '150px'});   // 弹出删除成功
                            //parent.reloadTablezs();// 父页面表格刷新
                            reloadTablezs();
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: "编辑 " + data.CONTACT,
                shadeClose: false,
                shade: [0.3],
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '90%'],
                scrollbar: false, //屏蔽滚动条
                content: '../../../../webhtml/customer/Customer_edit.html?type=edit&id=' + data.ID
            });
        } else if (obj.event === 'detail') {
            layer.open({
                type: 2,
                title: "查看 " + data.CONTACT,
                shadeClose: false,
                shade: [0.3],
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '90%'],
                scrollbar: false, //屏蔽滚动条
                content: '../../../../webhtml/customer/Customer_edit.html?type=detail&id=' + data.ID
            });
        }
    });
//============================租户管理
    //监听工具条  租户管理 编辑 删除 数据
    table.on('tool(zhgl)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确认删除 ' + data.BYNAME + ' 吗', {offset: '150px'}, function (index) {
                layer.close(index);
                $.ajax({
                    url: "/customer/deltenant",
                    data: {ID: data.ID, IS_DEL: 1},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
                            //reloadTable();    // 表格刷新
                            layer.msg(data.BYNAME + " 已删除", {offset: '150px'});   // 弹出删除成功
                            //parent.reloadTablezs();// 父页面表格刷新
                            reloadTablezs();
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: "编辑 " + data.BYNAME,
                shadeClose: false,
                shade: [0.3],
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '90%'],
                scrollbar: false, //屏蔽滚动条
                content: '../../../../webhtml/customer/Customer_edit_tenant.html?type=edit&id=' + data.ID
            });
        } else if (obj.event === 'detail') {
            layer.open({
                type: 2,
                title: "查看" + data.BYNAME,
                shadeClose: false,
                shade: [0.3],
                maxmin: true, //开启最大化最小化按钮
                area: ['900px', '90%'],
                scrollbar: false, //屏蔽滚动条
                content: '../../../../webhtml/customer/Customer_edit_tenant.html?type=detail&id=' + data.ID
            });
        }
    });
});
layui.use('table', function () {
    var table = layui.table;
    //监听单元格事件
    table.on('tool(building)', function (obj) {
        var data = obj.data;
        if (obj.event === 'setSign') {
            window.location.href = 'building/goRoom.do?&BUILDING_ID=' + data.BUILDING_ID + '&UNIT_ID=' + unitId;

        }
    });
});

/*
 *   打开新增合同的页面
 */
function toContractAdd() {
         layer.open({
                    type: 2,    //弹出类型
                    title: '新建贷款合同',   //标题
                    closeBtn: 1,    //显示关闭按钮
                    shade: [0.3],   //遮罩透明度
                    area: ['1000px', '90%'],  //打开窗口的宽高
                    time: 0,    //几秒后自动关闭，默认0时不关闭
                    anim: 1,    //动画效果
                    resize: false,  //设置成不允许拉伸
                    content: '../../../webhtml/contract/loan_add.html'  //iframe的url
                });
        $(this).blur(); //移除焦点，防止点击回车按钮造成多次打开
}

//表格刷新
function reloadTable() {
    table.reload('buildingTable', {});
}

//刷新收租列表
function reloadszlbTbale() {
    table.reload('szlbTbale', {});
}

//刷新招商
function reloadTablezs() {
    table.reload('zsgl', {});
}

//刷新招商
function reloadTablezh() {
    table.reload('zhgl', {});
}

//加载商圈
$.ajax({
    url: "/business/listBuss",
    data: {},
    dataType: "json",
    type: "post",
    async: false,
    success: function (res) {
        if (res.errcode === "0") {
            //载入楼宇数量
            for (var i = 0; i < res.data.length; i++) {
                /*$(".dropdown").append(" <li class=\"busid\"><a href=\"#\"><i class=\"icon-envelope icon-large\"></i>" + res.data[i].BUSINESS_NAME + "</a>" +
                    " <input type=\"hidden\" name=\"busid\" value=\"" + res.data[i].ID + "\" class=\"layui-busid\">" +
                    "</li>");*/
                //以上的注释是旧版首页
                $(".buss_div").append('<li class="nav-item">\n' +
                    '                           <a href="javascript:;"><i class="my-icon nav-icon icon_2 busid"><input type="hidden" name="busid" value="' + res.data[i].ID+'" /></i><span>' + res.data[i].BUSINESS_NAME+'</span><i class="my-icon nav-more"></i></a>\n' +
                    // '                           <input type="hidden" name="busid" value="' + res.data[i].ID +'" />\n' +
                    '                           <ul class="list___2Hba-">\n' +
                    '                               <li class="lia active___3jDSS">\n' +
                    '                                   <div class="imgBox___WeKZ4"\n' +
                    '                                        style="background-image: url(https://images.creams.io/defaultBuilding.png);"></div>\n' +
                    '                                   <div class="info___1Dhyq">\n' +
                    '                                       <div class="title___1pxg0">SOHO</div>\n' +
                    '                                       <div class="areaSize___1arhZ"><input type="hidden" value="test1"/>8.89 万㎡</div>\n' +
                    '                                   </div>\n' +
                    '                               </li>\n' +
                    '                           </ul>\n' +
                    '                       </li>');
            }
        } else if (res.errcode === "1") {
            layer.msg(res.errmsg, function () {
            });
        }
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.error(XMLHttpRequest.status);
        console.error(XMLHttpRequest.readyState);
        console.error(textStatus);
    }
});
/**
 * 选中 楼宇功能
 */
$(".lia").click(function () {
    clear();
    $(this).addClass("activebuilding");
});

function clear() {
    $(".lia").each(function () {
        if ($(this).hasClass("activebuilding")) {
            $(this).removeClass("activebuilding");
        }
    });
}

//切换集合 和 审批事项事项
$(".jh").click(function () {
    $(".main").show();
    $(".dbsx").hide();
    $(".dkht").hide();
    $(".layui-this").each(function () {
        if ("楼宇管理" == $(this).text()) {
            $(this).click();
        }
        if ("合同管理" == $(this).text()) {
            $(this).click();
        }
        if ("招商管理" == $(this).text()) {
            $(this).click();
        }
        if ("收入管理" == $(this).text()) {
            $(this).click();
        }
        if ("抄表管理" == $(this).text()) {
            $(this).click();
        }
    });
});
//加载  审批事项事项 ul li
$(".db").click(function () {
    $(".main").hide();
    $(".dbsx").show();
    $(".dkht").hide();
    $(".layui-this").each(function () {
        if ("合同类" == $(this).text()) {
            $(this).click();
        }
        if ("账单类" == $(this).text()) {
            $(this).click();
        }
    });
});
//加载贷款合同
$(".dk").click(function () {
    $(".main").hide();
    $(".dbsx").hide();
    $(".dkht").show();
    $(".layui-this").each(function () {
        if ("贷款管理" == $(this).text()) {
            $(this).click();
        }
        if ("招商管理" == $(this).text()) {
            $(this).click();
        }
        if ("合同管理" == $(this).text()) {
            $(this).click();
        }
        if ("收入管理" == $(this).text()) {
            $(this).click();
        }
    });
});

//主页消息数目
function messageCount() {
    var num = 0;
    $.ajax({
        url: "/sysMessage/messjson",
        data: {},
        dataType: "json",
        type: "post",
        async: false,
        success: function (res) {
            // console.log(res);
            if (res.errcode === "0") {
                if (res.data.length > 0) {
                    for (var i = 0; i < res.data.length; i++) {
                        if (res.data[i].colors == "1") {
                            num++;
                        }
                    }
                }
            } else if (res.errcode === "1") {
                layer.msg(res.errmsg, function () {
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest.status);
            console.error(XMLHttpRequest.readyState);
            console.error(textStatus);
        }
    });
    $.ajax({
        url: "/sysMessage/noticejson",
        data: {},
        dataType: "json",
        type: "post",
        async: false,
        success: function (res) {
            // console.log(res);
            if (res.errcode === "0") {
                if (res.data.length > 0) {
                    for (var i = 0; i < res.data.length; i++) {
                        if (res.data[i].IS_READ == "0") {
                            num++;
                        }
                    }
                }
            } else if (res.errcode === "1") {
                layer.msg(res.errmsg, function () {
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest.status);
            console.error(XMLHttpRequest.readyState);
            console.error(textStatus);
        }
    });
    if (num > 0) {
        setsmsSoundType();
    }
    $(".sysMessage").text(num);
}

messageCount();

//设置站内信声音提示类型
function setsmsSoundType() {
    $("#fhsmsobjsys").html('<audio style="display: none;" id="fhsmstsy" src="static/ujs/2.mp3" autoplay controls></audio>');
}

function loadUnit(activebuildingID) {
    $.ajax({
        url: "/building/info",
        data: {BUILDING_ID: activebuildingID},
        dataType: "json",
        type: "post",
        async: false,
        success: function (res) {
            if (res.errcode === "0") {
                var unitList = res.unitList;
                $("select[name='selectUnit']").empty();
                if (unitList.length > 0) {
                    for (var i = 0; i < unitList.length; i++) {
                        var str = '';
                        if (i == 0) {
                            str = '<option value="' + unitList[i].UNIT_ID + '" selected>' + unitList[i].UNIT_NAME + ' 单元</option>';
                            unitId = unitList[i].UNIT_ID;
                        } else {
                            str = '<option value="' + unitList[i].UNIT_ID + '">' + unitList[i].UNIT_NAME + ' 单元</option>';
                        }
                        $("select[name='selectUnit']").append(str);
                    }
                }
            } else if (res.errcode === "1") {
                layer.msg(res.errmsg, function () {
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest.status);
            console.error(XMLHttpRequest.readyState);
            console.error(textStatus);
        }
    });
    // 初始化渲染下拉框
    form.render();
    // 单元下拉框
    form.on('select(selectUnit)', function (data) {
        unitId = data.value;
        $(".main div ul li.layui-this").click();
        var loadbuilding = layui.data.loadbuild;
        loadbuilding.loadtotal();
    });
}

//首页左侧切换js
$(function(){
    // nav收缩展开
    $('.nav-item>a').on('click',function(){
        if (!$('.nav').hasClass('nav-mini')) {
            if ($(this).next().css('display') == "none") {
                //展开未展开
                $('.nav-item').children('ul').slideUp(300);
                $(this).next('ul').slideDown(300);
                $(this).parent('li').addClass('nav-show').siblings('li').removeClass('nav-show');
            }else{
                //收缩已展开
                $(this).next('ul').slideUp(300);
                $('.nav-item.nav-show').removeClass('nav-show');
            }
        }
    });
    //nav-mini切换
    $('#mini').on('click',function(){
        if (!$('.nav').hasClass('nav-mini')) {
            $('.nav-item.nav-show').removeClass('nav-show');
            $('.nav-item').children('ul').removeAttr('style');
            $('.nav').addClass('nav-mini');
        }else{
            $('.nav').removeClass('nav-mini');
        }
    });
});











